package cn.iocoder.yudao.module.trade.controller.admin.ordersafetycheck;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.trade.controller.admin.ordersafetycheck.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.ordersafetycheck.OrderSafetyCheckDO;
import cn.iocoder.yudao.module.trade.service.ordersafetycheck.OrderSafetyCheckService;

@Tag(name = "管理后台 - 安检单")
@RestController
@RequestMapping("/trade/order-safety-check")
@Validated
public class OrderSafetyCheckController {

    @Resource
    private OrderSafetyCheckService orderSafetyCheckService;

    @PostMapping("/create")
    @Operation(summary = "创建安检单")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check:create')")
    public CommonResult<Long> createOrderSafetyCheck(@Valid @RequestBody OrderSafetyCheckSaveReqVO createReqVO) {
        return success(orderSafetyCheckService.createOrderSafetyCheck(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新安检单")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check:update')")
    public CommonResult<Boolean> updateOrderSafetyCheck(@Valid @RequestBody OrderSafetyCheckSaveReqVO updateReqVO) {
        orderSafetyCheckService.updateOrderSafetyCheck(updateReqVO);
        return success(true);
    }

    @PutMapping("/update/list")
    @Operation(summary = "批量更新安检单")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check:update')")
    public CommonResult<Boolean> updateOrderSafetyCheckList(@Valid @RequestBody List<OrderSafetyCheckSaveReqVO> updateReqVO) {
        orderSafetyCheckService.updateOrderSafetyCheckList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除安检单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check:delete')")
    public CommonResult<Boolean> deleteOrderSafetyCheck(@RequestParam("id") Long id) {
        orderSafetyCheckService.deleteOrderSafetyCheck(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得安检单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check:query')")
    public CommonResult<OrderSafetyCheckRespVO> getOrderSafetyCheck(@RequestParam("id") Long id) {
        OrderSafetyCheckRespVO orderSafetyCheck = orderSafetyCheckService.getOrderSafetyCheck(id);
        return success(BeanUtils.toBean(orderSafetyCheck, OrderSafetyCheckRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得安检单分页")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check:query')")
    public CommonResult<PageResult<OrderSafetyCheckRespVO>> getOrderSafetyCheckPage(@Valid @RequestBody OrderSafetyCheckPageReqVO pageReqVO) {
        PageResult<OrderSafetyCheckRespVO> pageResult = orderSafetyCheckService.getOrderSafetyCheckPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出安检单 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderSafetyCheckExcel(@Valid OrderSafetyCheckPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderSafetyCheckRespVO> list = orderSafetyCheckService.getOrderSafetyCheckPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "安检单.xls", "数据", OrderSafetyCheckRespVO.class,
                        BeanUtils.toBean(list, OrderSafetyCheckRespVO.class));
    }

}